var path = require('path'),
	webpack = require('webpack'),
	fs = require('fs'),
	config = require('./config'),
	srcDir = path.join(process.cwd(), 'src'), // process.cwd()：返回运行当前脚本的工作目录的路径
	imagePath = path.join(srcDir, 'img'),
	cssPath = path.join(srcDir, 'css'),
	jsPath = path.join(srcDir, 'js'),
	viewsPath = path.join(srcDir, 'views'),
	HtmlWebpackPlugin = require('html-webpack-plugin'), //
	nodeModulesPath = path.join(__dirname, 'node_modules'),
	autoprefixer = require('autoprefixer'),
	pxtorem = require('postcss-pxtorem'), //px 处理
	bundleConfig = require('./build/bundle-config.json'),
	CopyWebpackPlugin = require('copy-webpack-plugin'),
	publicPath = config.domain + '/';
let port =  7809;
module.exports = {
	cache: true,
	// 入口
	entry: {
		index: [
			'react-hot-loader/patch', // 开启react代码的模块热替换（HMR）
			'webpack-dev-server/client?http://0.0.0.0:' + port, // 为webpack-dev-server的环境打包好运行代码,然后连接到指定服务器域名与端口
			'webpack/hot/only-dev-server', //然后连接到指定服务器域名与端口, only- 意味着只有成功更新运行代码才会执行热替换（HMR）
			path.join(__dirname, 'src/views/index.jsx')
		]
	},
	// 输出
	output: {
		publicPath: '/',
		path: path.join(__dirname, 'build/static/'), //文件输出目录
		filename: 'core/[name].js', //根据入口文件输出的对应多个文件名 。使用哈市命名，有利于文件更改清除打包文件不一致问题
		chunkFilename: 'core/[name].js'
	},
	resolve: {
		// 默认解析文件前缀
		extensions: ['.web.tsx', '.web.ts', '.web.jsx', '.web.js', '.ts', '.tsx', '.js', '.jsx', '.react.js','.json'],
		alias: { //配置别名，在项目中可缩减引用路径
			'img': path.join(srcDir, 'img'),
			'css': path.join(srcDir, 'css'),
			'js': path.join(srcDir, 'js'),
			'views': path.join(srcDir, 'views')
		}
	},
	devServer: {
		proxy: {
			'/api': {
				target: 'http://121.201.14.250:85',
				pathRewrite: {
					'^/api': ''
				}
			},
		},
		historyApiFallback: true,
		contentBase: 'build/static/',
		hot: true,
		inline: true,
		disableHostCheck: true,
		publicPath: '/',
		port: port,
	},
	module: {
		rules: [{
				test: /\.(eot|woff|woff2|ttf|svg|png|jpe?g|gif|mp4|webm)(\?\S*)?$/,
				exclude: [nodeModulesPath],
				include: [srcDir],
				loader: 'url-loader?limit=100&name=img/[name].[ext]',
			},
			{
				test: /\.json$/,
				loader: 'json-loader'
			},
			{
				test: /\.(scss|sass|css)$/,
				use: ['style-loader',
					{
						loader: 'css-loader',
						options: {
							sourceMap: true
						}
					},
					{
						loader: 'postcss-loader',
						options: {
							sourceMap: true,
							plugins: [autoprefixer, pxtorem({
								rootValue: 50,
								minPixelValue: 3,
								propWhiteList: [],
							})]
						}
					},
					{
						loader: 'sass-loader',
						options: {
							sourceMap: true,
							imagePath: imagePath,
							includePaths: [cssPath]
						}
					}
				]
			},
			{
				test: /\.(js|jsx)$/,
				exclude: [nodeModulesPath],
				include: [srcDir],
				loader: 'babel-loader',
			}
		]
	},
	plugins: [
		new webpack.HotModuleReplacementPlugin(), // 开起热替换模式
		new webpack.DllReferencePlugin({ // 加快webpack打包速度
			context: __dirname,
			manifest: require('./build/static/dll/vendor-manifest.json')
		}),
		new CopyWebpackPlugin([{
			from: path.resolve(__dirname, './src/js/lib'),
			to: path.resolve(__dirname, './build/static/lib')
		}]),
		new HtmlWebpackPlugin({
			template: path.join(__dirname, 'src/html/index.html'),
			bundleName: bundleConfig.vendor.js, // 打包生成的文件路径
			inject: true
		})
	]
};
